package com.company.lagou.homework.homework3;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client2 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 6666);
            System.out.println("客户端与服务器连接成功.");
            //向服务器发送的输出流
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            //读取控制台输入的信息
            BufferedReader brLocal = new BufferedReader(new InputStreamReader(System.in));

            BufferedReader brServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //服务让我们输入昵称
            System.out.println(brServer.readLine());
            String userName = brLocal.readLine();
            pw.println(userName);
            pw.flush();
            while (true){
                System.out.println("请输入发送给服务器的内容：");
                String localStr = brLocal.readLine();
                pw.println(localStr);
                pw.flush();
                System.out.println("服务返回的消息："+brServer.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
